package com.getpebble.android.jskit.webapps;

import android.content.Intent;
import android.os.Bundle;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.jskit.NoUiAppsList;
import com.getpebble.android.jskit.bridge.JsAppContextBridge;
import com.getpebble.android.jskit.webapps.PebbleWebappBaseActivity.WebappIntentTypeE;
import com.getpebble.android.jskit.webapps.builtins.NoUiBlankApp;
import java.util.Set;
import java.util.UUID;
import org.json.JSONObject;

public class PebbleNoUiWebappActivity extends PebbleWebappBaseActivity {
    protected void onDestroy() {
        PebbleWebappBaseActivity.LogSimpleDebug(this, "+++++++++++++++++ onDestroy", new Object[0]);
        super.onDestroy();
        try {
            NoUiAppsList.getInstance().noUiAppsListLockedAccess(this, NoUiWebappLifecycleOpsE.OnDestroy);
        } catch (Exception e) {
            Trace.debug("PebbleWebappBaseActivity", "onDestroy", e);
        }
    }

    protected void onCreate(Bundle savedInstanceState) {
        JsAppContextBridge.getInstance().setAppContext(getApplicationContext());
        onCreateWithView(savedInstanceState);
        PebbleWebappBaseActivity.LogSimpleDebug(this, "*************** onCreate", new Object[0]);
        super.onCreate(savedInstanceState);
        try {
            NoUiAppsList.getInstance().noUiAppsListLockedAccess(this, NoUiWebappLifecycleOpsE.OnCreate, savedInstanceState);
        } catch (Exception e) {
            Trace.debug("PebbleWebappBaseActivity", "onCreate", e);
        }
    }

    protected void onCreateWithView(Bundle savedInstanceState) {
        getWindow().addFlags(56);
    }

    protected void onResume() {
        super.onResume();
        try {
            NoUiAppsList.getInstance().noUiAppsListLockedAccess(this, NoUiWebappLifecycleOpsE.OnResume);
        } catch (Exception e) {
            Trace.debug("PebbleWebappBaseActivity", "onResume", e);
        }
        PebbleWebappBaseActivity.LogSimpleDebug(this, "+++++++++++++++++ onResume", new Object[0]);
        finish();
    }

    protected void onPause() {
        super.onPause();
        try {
            NoUiAppsList.getInstance().noUiAppsListLockedAccess(this, NoUiWebappLifecycleOpsE.OnPause);
        } catch (Exception e) {
            Trace.debug("PebbleWebappBaseActivity", "onPause", e);
        }
        PebbleWebappBaseActivity.LogSimpleDebug(this, "+++++++++++++++++ onPause", new Object[0]);
    }

    protected void onStop() {
        super.onStop();
        PebbleWebappBaseActivity.LogSimpleDebug(this, "+++++++++++++++++ onStop", new Object[0]);
    }

    public void onBackPressed() {
        finish();
    }

    protected boolean onNewWebappCreationIntent(Intent intent, String paramString) {
        Trace.debug("PebbleWebappBaseActivity", "onNewWebappCreationIntent");
        JSONObject jo = null;
        try {
            jo = new JSONObject(paramString);
        } catch (Exception e) {
            Trace.debug("PebbleWebappBaseActivity", "onNewWebappCreationIntent", e);
        }
        if (jo == null) {
            Trace.debug("PebbleWebappBaseActivity", "onNewWebappCreationIntent: jo is null");
            return true;
        }
        String webappObject = null;
        String loadUrlString = null;
        String appInfoJsonUrlString = null;
        boolean useBootstrapScripts = true;
        boolean quietLaunch = false;
        try {
            webappObject = jo.optString("webappObject", "");
            loadUrlString = jo.optString("loadUrlString", "");
            appInfoJsonUrlString = jo.optString("appInfoJsonUrlString", "");
            useBootstrapScripts = jo.optBoolean("useBootstrapScripts", true);
            String requestedByClientId = jo.optString("requestedByClientId", "");
            quietLaunch = jo.optBoolean("quietLaunch", false);
        } catch (Exception e2) {
            Trace.debug("PebbleWebappBaseActivity", "onNewWebappCreationIntent", e2);
        }
        if (!webappObject.equals(NoUiBlankApp.class.getCanonicalName())) {
            return quietLaunch;
        }
        Boolean isAlreadyLaunching = Boolean.TRUE;
        try {
            isAlreadyLaunching = (Boolean) NoUiAppsList.getInstance().noUiAppsListLockedAccessWithResult(this, new FindInWebappRunHostList(), loadUrlString);
        } catch (Exception e22) {
            Trace.debug("PebbleWebappBaseActivity", "onNewWebappCreationIntent", e22);
        }
        if (isAlreadyLaunching.booleanValue()) {
            Trace.warning("onNewWebappCreationIntent() for NoUiApp", "another instance of NoUiApp is already loading [" + loadUrlString + "] " + " so ignoring this Intent request");
            return quietLaunch;
        }
        NoUiBlankApp app = new NoUiBlankApp(this);
        NoUiAppsList.getInstance().noUiAppsListLockedAccess(this, new AddOnWebappRunHostList(app));
        app.onCreate(null);
        app.newAppInfoJsonUrl(appInfoJsonUrlString);
        if (useBootstrapScripts) {
            app.loadUrl(loadUrlString);
            return quietLaunch;
        }
        app.loadUrlNoBootstrapScripts(loadUrlString);
        return quietLaunch;
    }

    protected boolean onCloseKillWebappIntent(Intent causedByIntent, String paramString) {
        Trace.debug("PebbleWebappBaseActivity", "onCloseKillWebappIntent");
        JSONObject jo = null;
        try {
            jo = new JSONObject(paramString);
        } catch (Exception e) {
            Trace.debug("PebbleWebappBaseActivity", "onCloseKillWebappIntent", e);
        }
        if (jo == null) {
            Trace.debug("PebbleWebappBaseActivity", "onCloseKillWebappIntent: jo is null");
            return true;
        }
        String targetClientUuidString = null;
        boolean quietKill = false;
        try {
            targetClientUuidString = jo.optString("targetClientUuidString", "");
            quietKill = jo.optBoolean("quietKill", false);
        } catch (Exception e2) {
            Trace.debug("PebbleWebappBaseActivity", "onCloseKillWebappIntent", e2);
        }
        UUID sourceUuid = null;
        try {
            sourceUuid = UUID.fromString(targetClientUuidString);
        } catch (Exception e22) {
            Trace.debug("PebbleWebappBaseActivity", "onCloseKillWebappIntent", e22);
        }
        boolean deferredKillOfNoUiApp = false;
        Set<UUID> spawnedUuids = JsKit.jsKitAccess().getAllSpawnedClientsByGivenClientUuid(targetClientUuidString);
        if (spawnedUuids != null && spawnedUuids.size() > 0) {
            doIntentBasedCloseOfSpawnedClients(sourceUuid, (UUID[]) spawnedUuids.toArray(new UUID[spawnedUuids.size()]), 99);
            deferredKillOfNoUiApp = true;
        }
        if (deferredKillOfNoUiApp) {
            return quietKill;
        }
        doKillSequenceOnSpecifiedClientUuid(causedByIntent, targetClientUuidString);
        return quietKill;
    }

    private Boolean doIntentBasedCloseOfSpawnedClients(UUID sourceUuid, UUID[] uuids, final int tid) {
        final Intent outIntent = new Intent(this, PebbleWebUiWebappActivity.class);
        PebbleWebappBaseActivity.modifyIntentForCloseMultipleWebapps(outIntent, uuids);
        PebbleWebappBaseActivity.modifyIntentWithRequestedByClientUuid(outIntent, sourceUuid, WebappIntentTypeE.CloseKill);
        runOnUiThread(new Runnable() {
            public void run() {
                PebbleNoUiWebappActivity.this.startActivityForResult(outIntent, tid);
            }
        });
        return Boolean.TRUE;
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        String closeKillBaseNoUiWebappParams = null;
        try {
            closeKillBaseNoUiWebappParams = data.getStringExtra("PEBBLE_WEBAPP_ACTIVITY_CLOSEKILL_WEBAPP_PARAMS");
        } catch (NullPointerException e) {
            Trace.debug("PebbleWebappBaseActivity", "onActivityResult", e);
        }
        JSONObject jo = null;
        try {
            jo = new JSONObject(closeKillBaseNoUiWebappParams);
        } catch (Exception e2) {
            Trace.debug("PebbleWebappBaseActivity", "onActivityResult", e2);
        }
        if (jo == null) {
            finish();
            return;
        }
        String targetClientUuidString = null;
        try {
            targetClientUuidString = jo.optString("targetClientUuidString", "");
        } catch (Exception e22) {
            Trace.debug("PebbleWebappBaseActivity", "onActivityResult", e22);
        }
        if (targetClientUuidString == null) {
            finish();
            return;
        }
        doKillSequenceOnSpecifiedClientUuid(data, targetClientUuidString);
        finish();
    }
}
